package animationIntro;

import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import screenIntroduction.Screen;

public class Blerg {

    public static void main(String args[]) {
	DisplayMode displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
	Blerg b = new Blerg();
	b.run(displayMode);
    }
    
    private Screen screen;
    private Image bg;
    private Animation a;
    
    //loads pictures to animation
    public void loadPics() {
	bg = new ImageIcon("resources/background.png").getImage();
	Image face1 = new ImageIcon("resources/face_frown.png").getImage();
	Image face2 = new ImageIcon("resources/face_gasp.png").getImage();
	a = new Animation();
	a.addScene(face1, 250);
	a.addScene(face2, 250);
    }
    
    //main run engine
    public void run(DisplayMode dm) {
	screen = new Screen();
	try {
	    screen.setFullScreen(dm,  new JFrame());
	    loadPics();
	    movieLoop();
	} finally {
	    screen.restoreScreen();
	}
    }
    
    //movie looping
    public void movieLoop() {
	long startingTime = System.currentTimeMillis();
	long cumulativeTime = startingTime;
	while (cumulativeTime - startingTime < 5000) {
	    long timePassed = System.currentTimeMillis() - cumulativeTime;
	    cumulativeTime += timePassed;
	    a.update(timePassed);
	    
	    Graphics g = screen.getFullScreenWindow().getGraphics();
	    draw(g);
	    g.dispose();
	    try {
		Thread.sleep(20);
		//gives rest time to catch up
	    } catch (Exception ex) {
		ex.printStackTrace();
	    }
	}
    }
    
    public void draw(Graphics g) {
	g.drawImage(bg, 0, 0, null);
	g.drawImage(a.getImage(), 0, 0, null);
    }
    
}
